package com.feng.springboot.controller;

import com.feng.springboot.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

//    @Autowired
    StringRedisTemplate redisTemplate;


    /**
     * 去往登录页面
     */
    @GetMapping(value = {"/","/login"})
    public String loginPage(){
        return "login";
    }

    /**
     * 2021-11-11 15:46:30.996 ERROR 15416 --- [nio-8087-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    :
     * Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request
     * processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving
     * template [index], template might not exist or might not be accessible by any of the configured Template
     * Resolvers] with root cause
     *
     * org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index], template might not
     * exist or might not be accessible by any of the configured Template Resolvers\
     *
     * 解决办法：添加 xmlns:...
     *  <!DOCTYPE html>
     * <html lang="en" xmlns:th="http://www.thymeleaf.org">
     * @return
     */
    @PostMapping("/index")
//    @ResponseBody
//    public String main(String name,String password){
    public String main(User user, Model model, HttpSession session){
        System.out.println(user.getUsername()+","+user.getPassword());
        String username = user.getUsername();
        String password = user.getPassword();
        if (!StringUtils.isEmpty(username)&&"123456".equals(password)){
            session.setAttribute("loginUser",user);
            return "redirect:/index";
        }
        else {
            model.addAttribute("msg","账号密码错误");
            return "login";
        }

//        return "success";
//        return "";
    }

    /**
     * 防止刷新页面的时候表单重复提交，这样只是刷新index页面
     * @return
     */
    @GetMapping("/index")
    public String toIndex(HttpSession session,Model model){
        Object loginUser = session.getAttribute("loginUser");
        /*if (loginUser!=null){
            return "index";
        }
        else {
            model.addAttribute("msg","请重新登录");
            return "login";
        }*/

        /**
         *
         * 如果项目没有使用redis的时候，下面的几行调用必须要禁用掉，不然一直报错
         * 2021-11-16 15:39:31.761 ERROR 1404 --- [nio-8087-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379] with root cause
         *
         * java.net.ConnectException: Connection refused: no further information
         * 	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_41]
         * 	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_41]
         */
        //将访问次数放到了缓存中
//        ValueOperations<String, String> operations = redisTemplate.opsForValue();
//        String s = operations.get("/index");
//        model.addAttribute("indexCount",s);
//        System.out.println("index页面访问次数："+s);
        return "index";
    }
}
